package com.bringway.p2p.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.bringway.framework.controller.JsonController;
import com.bringway.framework.util.RequestUtils;
import com.bringway.global.domain.SysUserDetails;
import com.bringway.p2p.service.SmrzjlService;
/**
 * 
 * @author qjy
 * @version 创建时间：2016年06月14日 上午13:15:42
 * @功能：异常查询
 */
@Controller
@RequestMapping("/smrzjl")
public class SmrzjlController  extends JsonController {
	Logger logger = Logger.getLogger(PosController.class);
	@Autowired
	private SmrzjlService smrzjlService; 
	/**
	 * 
	 * @param request
	 * @param response 查询数据
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/querySmrzjlList")
	public ModelAndView querySmrzjlList(HttpServletRequest request,HttpServletResponse response) throws Exception{
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		Integer page = RequestUtils.getIntParameter(request, "page");//新版本,传参
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		if (null!=page) {//新版本,传参
			start=(page-1)*limit;
		}
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		String username= RequestUtils.getStringParameter(request, "username");
		String sjhm= RequestUtils.getStringParameter(request, "sjhm");
		String rzzt= RequestUtils.getStringParameter(request, "rzzt");
		
		params.put("start", start);
		params.put("limit", limit);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("username", username);
		params.put("sjhm", sjhm);
		params.put("rzzt", rzzt);
		List<Map<String, Object>> list = smrzjlService.querySmrzjlList(params);
		int count = smrzjlService.gettrzCount(params);
		result.put("data", list);	
		result.put("total", count);
		if (null!=page) {//新版本,传参
			int totalPage=count%limit==0?count/limit:count/limit+1;
			result.put("totalPage", totalPage);
		}
		serialize(request,response,result);
		return null;
	}
	/**
	 * @方法名称:del
	 * @方法描述:删除实名认证记录
	 * @author qjy
	 * @date 2016年09月14日上午11:21
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/delsmrzjl")
	public ModelAndView delsmrzjl(HttpServletRequest request,HttpServletResponse response) throws Exception{
		SysUserDetails sysUserDetails =	(SysUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		String Id = RequestUtils.getStringParameter(request, "d_id");
		String[] Ids=Id.split(",");
		params.put("gyh", sysUserDetails.getGyh());
		try{
			for (String string : Ids) {
				params.put("Id", string);
				Map<String,Object> delsmrzjl = smrzjlService.getsmrzjl(params);
				if(null !=delsmrzjl){
					if(delsmrzjl.containsKey("VERIFY_STATE") && "02".equals(delsmrzjl.get("VERIFY_STATE"))){
						smrzjlService.delsmrzjl(params);
						result.put("isDeleteSuc", true);
						result.put("msg", "删除成功");
					}else{
						result.put("isDeleteSuc", false);
						result.put("msg", "对不起,选中信息中有已认证成功信息无法删除!");
					}
				}else{
					result.put("isDeleteSuc", false);
					result.put("msg", "对不起,信息不存在!");
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			result.put("isDeleteSuc", false);
			result.put("msg", "对不起,连接超时!");
			logger.info("delsmrzjl 删除实名认证记录失败！"+ params.toString());
		}
		result.put("success", true);
		serialize(request,response,result);
		return null;
	}

	
}
